import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {

    state = {mouse:false}//标识鼠标移入、移出

    // 鼠标移入、移出的高阶回调
    handleMouse = (flag) =>{
        return () => {
        //   console.log(flag)
        this.setState({mouse:flag})
        }
    }

    // 勾选或取消勾选的某一个todo回调
    handleCheck = (id)=>{
        return (event) =>{
            // console.log(id,event.target.checked);
            this.props.updatetodo(id,event.target.checked)
            
        }
    }


    // 删除一个todo的回调
    handleDelete = (id)=>{
        if(window.confirm('确定删除吗？')){
            
            this.props.deleteTodo(id)
        }
    }

    render() {
        const {id,name,done } = this.props
        const {mouse} = this.state
      

        return (
            <li onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)} style={{backgroundColor:mouse ? '#ddd':'#fff'}} >
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleCheck(id)} />
                    <span>{name}</span>
                </label>
                <button onClick={()=>{this.handleDelete(id)}} className="btn btn-danger" style={{display:mouse ? 'block':'none'}}>删除</button>
            </li>
        )
    }
}
